<?php
if(!defined('IN_IBEIKE')) {
	exit('Access Denied');
}

function getAtlist($data){
	
}
function outJson($in){
	if(is_array($in)){
		$json=common_convert($in);
		$json->convert();
		return $json->output();
	}else{
		return false;
	}
	
}

function getweather(){
	global $_G;
	$cityinfo=array();
	$res=dfsockopen("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=202.204.49.82&".$_G['clientip']);
	if($res){
		$res=substr($res,21,-1);
		$cityinfo=json_decode($res,true);
		if($cityinfo['ret']){
			$city=$cityinfo['city'];
			$weather=dfsockopen("http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=84abac1168ea72a1cc74828747852a5b");
			$weather=json_decode($weather,true);
			if(!$weather['error']){
				return array('city'=>$city,'pic'=>$weather['results'][0]['weather_data'][0]['dayPictureUrl'],'date'=>date('m/d'),'weather'=>$weather['results'][0]['weather_data'][0]['weather'],'temperature'=>$weather['results'][0]['weather_data'][0]['temperature']);
			}else{
				return '获取失败';
			}
		}
	}else{
		return '暂无信息';
	}
}
function import_value_twitter_field($tid,$authorid,$author,$message,$dateline,$client,$ttype){//暂无评论数等
	if(isset($tid,$authorid,$author,$message,$dateline,$client,$ttype)){
		return false;
	}
	$data=array($tid,$authorid,$author,$message,$dateline,$client,$ttype);
	return $data;
}
function check_massage_lenthtype($massage){
	if(isset($massage)){
		$lenth=strlen($massage);
		if($lenth<=200){
			return 1;
		}else{
			return 2;
		}
	}
	return false;

}
function random_number($begin=0,$end=29,$limit=10){//获取1到30之间的10条随机数
	$rand_array=range($begin,$end);
	shuffle($rand_array);//调用现成的数组随机排列函数
	return array_slice($rand_array,0,$limit);//截取前$limit个
}
function extract_http_from_massage($massage){//获取网页链接//已测试
	if(!empty($massage)){
		if(substr_count($massage, 'http')!=0){
			$time=substr_count($massage, 'http')."<br/>";
			$len=strlen($massage);
			$l=1;
			$i=1;
			$j=1;
			for($i;$i<=$time;$i++){
				$b=stripos($massage,"http",$l);//返回http在$massage中的第n次出现的位置
				$http="";
				for($j=$b;$j<=$len;$j++){
					if($massage{$j}==" "){//这里验证字符是否为汉字或空格
						//org($massage{$j})>=0 && org($massage{$j})<=255 ||
						break;
					}else{
						$http.=$massage{$j};
						//echo $http."<br/>";
					}
						
				}
				//$http="";//这里的$http是识别出来的网页链接
				$cut=$j-$b;
				//echo $j."<br>";
				//echo $http."<br/>";
				//$httpchage=$http.$http;
				$httpchage="<a href='".$http."'>".$http."</a>";
				//echo "httpchageto: ".$httpchage."<br/>";
				$httplen=strlen($httpchage);
				$massage=substr_replace($massage,$httpchage,$b,$cut);//将http替换为a标签
				//echo $massage."<br/>";
				$len=strlen($massage);
				//echo $len."<br/>";
				$l=$httplen+$b+1;//获取新的massage的已处理的http的结尾>的位置
				//echo $l."<br/>";
			}
			return $massage;
		}else{
			return $massage;
		}
	}else{
		return false;
	}

}
function extract_call_from_massage($massage){//获取@的用户名并修改massage//已测试
	if(!empty($massage)){
		if(substr_count($massage, '@')!=0){
			$i=1;
			$j=1;
			$l=1;
			$len=strlen($massage);
			$time=substr_count($massage, '@');
			for($i;$i<=$time;$i++){
				$b=strpos($massage,"@",$l);//返回http在$massage中的第n次出现的位置
				$len=strlen($massage);
				for($j=$b;$j<=$len;$j++){
					if($massage{$j}!=" "){
						
					}else{
						break;
					}
						
				}
				$cut=$j-$b;
				$callchage="";
				$call=substr($massage,$b+1,$cut-1);
				$callchage="<a href='http://baidu.com'>".$call."</a>";
				$calllen=strlen($callchage);
				$massage=substr_replace($massage,$callchage,$b+1,$cut);//将@替换为a标签
				$l=$calllen+$b;
			}
			return $massage;
		}else{
			return $massage;
		}
	}else{
		return false;
	}
}
function extract_expression_from_massage($massage){//识别表情,在massage输出的时候使用//已测试
	//$ex_array=C::t('common_smiley')->put_bbcode_add_to_array();//这里要取得所有表情在[]内的字符�?为一个数�?数组的�?为表情标�?键为表情图片的名�?
	//$ex_array_flip=array_flip($ex_array);
	if(!empty($massage)){
	$len = strlen($massage);//取得信息的长�?
	$countright = substr_count($massage, '[');//返回[�?massage内出现的次数
	$countleft = substr_count($massage, ']');//返回]�?massage内出现的次数
	if($countright=0 || $countleft=0){
	return $massage;
	}else{
	//$left=$right=0;
		$i=1;
		$j=1;
		$l=1;
		$b=strpos($massage,"[",$l);
		//echo $b."<br/>";
		$time=substr_count($massage, '[');
		//echo $time."<br/>";
		while($i<=$time){
		$b=strpos($massage,"[",$l);
			//echo $b."<br/>";
			if(($b+1)<=$len){
			for($j=$b+1;$j<=$len;$j++){
			if($massage{$j}=="]"){
				//echo "for".$i."time: ".$massage{$j-1}."<br/>";
				$cut=$j-$b+1;
					$inside=substr($massage,$b,$cut);
					//echo $inside."<br/>";
					$inside_side=substr($inside,1,-1);//去除兩邊的[]
					//if(inarray($inside_side,$ex_array)){//如果[]内的字符串符合数组内的表�?则替换[]以及里面的内容为img标签
					//$imgcode=$ex_array_flip["$inside"];//获取图片的代码
					//$inside_chage="<img src='/**/".$imgname."' alt='".$inside_side."' >";
					$inside_chage="<a href='http://www.baidu.com'>".$inside."</a>";
					$newlen=strlen($inside_chage);
					$massage=substr_replace($massage,$inside_chage,$b,$cut);
					$len=strlen($massage);
							$l=$b+$newlen+1;//信息已更换并且指针指到更换后的img标签的后�?��
							//echo $l."<br/>";
					break;
					//}

				}
				}
				}
				//if($massage{$i}=="["){

				//}
				$i++;
				}
				return $massage;
				}


				}else{
				return false;
				}
				}


function find_first_tag_from_msg($massage){//将字符串里的第一条话题按tagid和tagname提取到数组//已测试
				if(!empty($massage)){
				    if(substr_count($massage,"#")>=2){
				        $len=strlen($massage);
				        $i=$j=1;
				        $b=stripos($massage,"#");
				            for($i=$b+1;$i<$len;$i++){
				                if($massage{$i}=="#" && $i!=$b+1){
					                $cut=$i-$b;
					                $tagname=substr($massage,$b,$cut+1);//取得##字符
						            $taghash=md5($tagname);
						            $insert_tag=array();
						            $insert_tag['tagid']=$tagid;
						            $insert_tag['tagname']=$tagname;
						            C::t('common_tag')->insert_tag_by_tagname($tagname,$taghash);
						            break;
						        }
                            }
						    return $insert_tag;
                        }else{
						    return 0;
						}
				}else{
					return false;
			}
}
function insert_tag_to_array($massage){//将字符串里的所有话题按tagid和tagname写入数组//已测试为处理##的情况
						if(!empty($massage)){
						if(substr_count($massage, '#')>=2){
						$i=1;
						$j=1;
						$l=1;
						$insert_tag=array();
						$len=strlen($massage);
						$time=round(substr_count($massage, '#')/2-0.2);
							for($i;$i<=$time;$i++){
							$b=strpos($massage,"#",$l);//返回#在$massage中的第n次出现的位置
							$len=strlen($massage);
							for($j=$b+1;$j<=$len;$j++){
							if($massage{$j}=="#"){
							break;
							}
										
							}
							$cut=$j-$b+1;
								$tagname=substr($massage,$b,$cut);
								$tagid=$i;//暂时未知
								$taghash="haha";//暂时未知
								C::t('common_tag')->insert_tag_by_tagname($tagid,$tagname,$taghash);
										$insert_tag[$tagid]['tagid']=$tagid;
										$insert_tag[$tagid]['tagname']=$tagname;
										$insert_tag[$tagid]["taghash"]=$taghash;
										$l=$j+1;
							}
							return $insert_tag;
							}else{
			return 0;
							}
							}else{
							return false;
							}
							}
function extract_tag_from_massage($massage){//识别话题,在massage输出的时候使用//已测试,但未考虑##中无值得情况
							if(!empty($massage)){
									if(substr_count($massage, '#')>=2){
									$i=1;
									$j=1;
									$l=1;
									$len=strlen($massage);
									$time=round(substr_count($massage, '#')/2-0.2);
										for($i;$i<=$time;$i++){
										$b=strpos($massage,"#",$l+1);//返回#在$massage中的第n次出现的位置
										$len=strlen($massage);
										for($j=$b+1;$j<=$len;$j++){
										if($massage{$j}=="#"){
										break;
										}
										}
										$cut=$j-$b+1;
										$callchage="";
										$call=substr($massage,$b,$cut);
										$callchage="<a href='http://www.baidu.com'>".$call."</a>";
										$calllen=strlen($callchage);
										$massage=substr_replace($massage,$callchage,$b,$cut);//将##替换为a标签
										$l=$calllen+$b;
			}
			return $massage;
										}else{
										return $massage;
										}
										}else{
										return false;
										}
										}
										function deal_title_from_long($massage){//从长文章中提取标题
										if(!empty($massage)){

}else{
return false;
}
}




function check_massage_validity($massage){
	if(!empty($massage)){

	}

}
function processsquare($square){//处理原始数据
	include_once libfile('function/bbcode');
	foreach($square as $key=>$val){
		$square[$key]['headimg']=avatar($val['authorid'],'small',false,false,'../ucenter');
		$square[$key]['time']=gmdate('Y-m-d H:i:s',$val['dateline']);
		if($square[$key]['typeid']=='2'){
			$square[$key]['spec']=unserialize($val['svalues']);
		}elseif($square[$key]['typeid']=='5'){
			$square[$key]['spec']=unserialize($val['svalues']);
			foreach($square[$key]['spec'] as $k=>$v){
				$square[$key]['spec'][$k]=urldecode($v);
			}
		}
		parsesmiles($square[$key]['message']);
	}
	return $square;
}
function processreply($reply){//处理原始回复数据
	include_once libfile('function/bbcode');
	foreach($reply as $key=>$val){
		$reply[$key]['headimg']=avatar($val['ruid'],'small',false,false,'../ucenter');
		$reply[$key]['time']=gmdate('Y-m-d H:i:s',$val['dateline']);
		$maxrid=$reply[$key]['rid'];
	}
	return array($reply,$maxrid);
}

function getreply($rid){
	if($rid){
		$subreply=C::t('city_reply')->fetch_by_rid($rid);
		$subreply['headimg']=avatar($subreply['ruid'],'small',false,false,'../ucenter');
		$subreply['time']=gmdate('Y-m-d H:i:s',$subreply['dateline']);
		if(!empty($subreply)){
			$ret=<<<EOT
			<li>
			<div id='rid_{$subreply["rid"]}' class="reply-sub">
			<div class="reply-headimg">
			{$subreply['headimg']}
			</div>
			<div class="reply-detail">
			<div class="reply-username">
			<a href="#"></a>
			</div>
			<div class="reply-text">
			{$subreply['message']}
			</div>
			<div class="reply-bottom">
			<div class="reply-time">{$subreply['time']}</div>
			<div class="reply-stat">
			<ul class="nav nav-pills nav-sm nav-nobg">
			<li><a class="send_nice" href="javascript:void(0);" data="{&quot;type&quot;:&quot;rid&quot;,&quot;id&quot;:{$subreply['rid']}}">赞(0)</a></li>
			<li><a>回应(0)</a></li>
			</ul>
			
			</div>
			<div style="clear:both"></div>
			</div>
			</div>
			</div>
			</li>
EOT;
			return $ret;
		}
	}
}

?>
